package com.hsytech.baoinbao;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.BaseExpandableListAdapter;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;


import com.hsytech.baoinbao.gsondata.RecyclingManStockData;
import com.tonicartos.widget.stickygridheaders.StickyGridHeadersSimpleAdapter;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;

/**
 * Created by 123 on 2016/1/28.
 */
public class AdapterListView extends BaseExpandableListAdapter {

//    private SparseArray<SparseArray<HeaderViewHolder>> cache = new SparseArray<SparseArray<HeaderViewHolder>>();

    private List<RecyclingManStockData.Storage> listData;
    private List<RecyclingManStockData.Storage> listDataExt;
    private Context mContext;
    private LayoutInflater mInflater;
    private List<String> editList = new ArrayList<>();

    public AdapterListView(Context context, RecyclingManStockData src, RecyclingManStockData dest) {
        listData = src.getResult().getStorage();
        listDataExt = dest.getResult().getStorage();

        mContext = context;
        mInflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    public List<String> getEditList() {
        return editList;
    }

    //得到子item需要关联的数据
    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return (listData.get(childPosition).getChildren());
    }

    //得到子item的ID
    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    //设置子item的组件
    @Override
    public View getChildView(final int groupPosition, final int childPosition,
                             boolean isLastChild, View convertView, ViewGroup parent) {
        HeaderViewHolder headerViewHolder = null;

        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.item_02_waste_list, null);
            headerViewHolder = new HeaderViewHolder();
            headerViewHolder.init(convertView);
        } else {
            headerViewHolder = (HeaderViewHolder) convertView.getTag();
        }
        headerViewHolder.setData(listData.get(groupPosition).getChildren().get(childPosition), listDataExt.get(groupPosition).getChildren().get(childPosition));
        return convertView;
    }

    //获取当前父item下的子item的个数
    @Override
    public int getChildrenCount(int groupPosition) {
        return listData.get(groupPosition).getChildren().size();
    }

    //获取当前父item的数据
    @Override
    public Object getGroup(int groupPosition) {
        return listData.get(groupPosition);
    }

    @Override
    public int getGroupCount() {
        return listData.size();
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    //设置父item组件
    @Override
    public View getGroupView(int groupPosition, boolean isExpanded,
                             View convertView, ViewGroup parent) {
        convertView = mInflater.inflate(R.layout.item_01_waste_list, null);
        TextView firstName = (TextView) convertView.findViewById(R.id.firstName);
        firstName.setText(listData.get(groupPosition).getName());
        return convertView;
    }

    @Override
    public boolean hasStableIds() {
        return true;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }

    public class HeaderViewHolder {

        public RecyclingManStockData.Children src, dest;

        public TextView name, name_center, name_right;

        public EditTextExt edit;

        public HeaderViewHolder() {

        }

        public void init(View convertView) {
            convertView.setTag(this);

            this.name = (TextView) convertView.findViewById(R.id.name);
            this.name_center = (TextView) convertView.findViewById(R.id.name_center);
            this.name_right = (TextView) convertView.findViewById(R.id.name_right);
            this.edit = (EditTextExt) convertView.findViewById(R.id.edit);
            if (this.edit.getText().toString().equals("0")) {
                edit.setTextColor(mContext.getResources().getColor(R.color.common_title_color));
            } else {
                edit.setTextColor(mContext.getResources().getColor(R.color.red));
            }
            this.edit.setOnTextChangedListener(new EditTextExt.OnTextChangedListener() {
                @Override
                public void beforeTextChanged(EditTextExt editTextExt, CharSequence s, int start, int count, int after) {

                }

                @Override
                public void afterTextChanged(EditTextExt editTextExt, Editable s) {

                    if (editTextExt.getText().toString().equals("0")) {
                        edit.setTextColor(mContext.getResources().getColor(R.color.common_title_color));

                    } else {

                        edit.setTextColor(mContext.getResources().getColor(R.color.red));

                    }

                    if (edit.getText().toString().length() >= 2) {
                        if (String.valueOf(edit.getText().toString().charAt(0)).equals("0")) {
                            s.delete(0, 1);
                        }
                    }


                }

                @Override
                public void onTextChanged(EditTextExt editTextExt, CharSequence s, int start, int count) {

                    dest.setStock(editTextExt.getText().toString());
                }
            });

        }

        public void setData(RecyclingManStockData.Children src, RecyclingManStockData.Children dest) {
            this.src = src;
            this.dest = dest;
            this.edit.setText(dest.getStock());
            this.name.setText(src.getCat_name());
            this.name_center.setText("[" + src.getUnit() + "]");
            this.name_right.setText("(实际库存" + src.getStock() + src.getUnit() + ")");
        }
    }
}
